package com.spring.theory.bean.beanFactoryPostProcessor.postProcessor;

import java.util.Set;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.type.MethodMetadata;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;

/**
 * TestBeanPostProcessor
 *
 * @author H
 * @version 1.0
 * @date 2023-12-26 15:09
 */
@Slf4j
public class TestBeanPostFactoryProcessor implements BeanDefinitionRegistryPostProcessor {

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        try {
            CachingMetadataReaderFactory factory = new CachingMetadataReaderFactory();
            //MetadataReader 获取类信息是不加载类的，效率高于反射
            MetadataReader metadataReader = factory.getMetadataReader(
                    new ClassPathResource("com/spring/theory/bean/beanFactoryPostProcessor/model/Config.class"));
            //找到被@Bean注解标注的方法信息
            Set<MethodMetadata> annotatedMethods = metadataReader.getAnnotationMetadata().getAnnotatedMethods(Bean.class.getName());
            for (MethodMetadata method : annotatedMethods) {
                log.info("自定义@Bean注解的工厂后处理器：{}", method);
                //@Bean的内属性 可以通过 method.getAnnotationAttributes 获取到
                String initMethod = method.getAnnotationAttributes(Bean.class.getName()).get("initMethod").toString();
                /**
                 * 定义@Configuration注解bean内的方法，配置类bean可看作是一个bean工厂；如MapperFactoryBean工厂bean
                 * 生成工厂bean 不需要在bean定义中指定类名
                 */
                BeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition()
                        //config作为工厂bean，里面的每个bean作为工厂方法
                        .setFactoryMethodOnBean(method.getMethodName(), "config")
                        //针对于工厂方法、构造方法参数的自动装配；指定为构造器模式
                        .setAutowireMode(AbstractBeanDefinition.AUTOWIRE_CONSTRUCTOR)
                        //绑定@Bean注解内的方法或者字段
                        .setInitMethodName(!initMethod.isEmpty() ? initMethod : null)
                        .getBeanDefinition();
                registry.registerBeanDefinition(method.getMethodName(), beanDefinition);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
}
